package com.example.todolist.controller;


import com.example.todolist.dto.business.PlanStatusCountDTO;
import com.example.todolist.model.SysUser;
import com.example.todolist.model.TeamFarDO;
import com.example.todolist.model.TeamPerDO;
import com.example.todolist.model.TeamPlanDO;
import com.example.todolist.response.BaseResponse;
import com.example.todolist.response.SystemResponse;
import com.example.todolist.service.SysUserService;
import com.example.todolist.service.TeamFarService;
import com.example.todolist.service.TeamPerService;
import com.example.todolist.service.TeamPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cqq
 * @since 2022-03-21
 */
@RestController
@RequestMapping("/team")
@CrossOrigin
//@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "事项模块", value = "事项controller")
public class TeamController {
    @Autowired
    private TeamFarService teamFarService;
    @Autowired
    private TeamPlanService teamPlanService;
    @Autowired
    private TeamPerService teamPerService;
    @Autowired
    private SysUserService sysUserService;


    @ApiOperation("新增协助事项")
    @PostMapping("/add")
    public BaseResponse addTeam(@RequestBody TeamFarDO teamFarDO){
//        List<TeamFarDO> farDOList = teamFarService.selectByOneTeam(teamFarDO.getUid());
//        if (farDOList.size()>=1) {
//            SystemResponse.fail("已有团队！！");
//        }
        String teamId = UUID.randomUUID().toString();
        SysUser sysUser = sysUserService.selectByOpenID(teamFarDO);
        teamFarDO.setUname(sysUser.getNickname());
        teamFarDO.setUurl(sysUser.getAvatar());
        BaseResponse one =  teamFarService.addTeam(teamFarDO,teamId);
//       BaseResponse two = teamPlanService.addTeamPlan(teamFarDO.getPlanDOS(), teamId);
        if (one.getCode() == 200 ) {
            return SystemResponse.success();
        } else {
           return SystemResponse.fail("新增协助失败！！");
        }
    }

    @ApiOperation("加入事项")
    @PostMapping("/joinTeam")
    public BaseResponse joinTeam(@RequestBody TeamFarDO teamFarDO){
        SysUser sysUser = sysUserService.selectByOpenID(teamFarDO);
        TeamFarDO one =  teamFarService.joinTeam(teamFarDO);
        if (one == null) {
            return SystemResponse.fail("邀请码错误！！");
        }
        one.setUname(sysUser.getNickname());
        one.setUurl(sysUser.getAvatar());
        one.setUid(teamFarDO.getUid());
        if (one.getCode() != null) {
            List<TeamPerDO> teamPerDOS = teamPerService.IsJoinTeam(one.getTeamId(), teamFarDO.getUid());
            if (teamPerDOS.size() >= 1) {
                return SystemResponse.fail("已加入该事项，无须重新加入！！");
            }
            int i = teamPerService.addPerToTeam(one);
            if (i > 0) {
                List<TeamPerDO> teamPerDOList = teamPerService.selectDetails(teamFarDO.getTeamId());
//                List<TeamPlanDO> teamPlanDOList = teamPlanService.selectDetails(teamFarDO.getTeamId());
                teamFarDO.setPerDOList(teamPerDOList);
//                teamFarDO.setPlanDOS(teamPlanDOList);
                return SystemResponse.success(teamFarDO);
            } else {
                return SystemResponse.fail("加入失败！！");
            }
        } else {
            return SystemResponse.fail("邀请码错误！！");
        }
    }

    /**
     * 废弃
     * @return
     */
    @ApiOperation("修改团队人员的事项")
    @PostMapping("/update_team_plan_by_person")
    public BaseResponse updatePlan(@RequestBody TeamPerDO teamPerDO){
        boolean b = teamPerService.updateById(teamPerDO);
        if (b) {
            return SystemResponse.success();
        } else {
            return SystemResponse.fail("修改事项失败！！");
        }
    }

    /**
     * 废弃
     * @param
     * @return
     */
    @ApiOperation("查询全部团队")
    @GetMapping("/selectAll")
    public  BaseResponse selectAll(){
        List<TeamFarDO> teamFarDOS = teamFarService.selectAll();
        return SystemResponse.success(getTeamFarDOS(teamFarDOS),getTeamFarDOS(teamFarDOS).size());
    }

    @ApiOperation("查询事项详情")
    @GetMapping("/selectDetails")
    public BaseResponse  selectDetails(@RequestParam String id){
        TeamFarDO teamFarDO = teamFarService.selectDetails(id);
//        if (teamFarDO == null) {
//            List<TeamPerDO> teamPerDOS = teamPerService.selectByOneJoinTeam(openid);
//            if (teamPerDOS.size() >= 1) {
//                teamFarDO = teamFarService.selectDetailsByTeamID(teamPerDOS.get(0).getTeamId());
//                if (teamFarDO == null) {
//                    return SystemResponse.success(null);
//                }
//            } else {
//                return SystemResponse.success(null);
//            }
//        }
        List<TeamPerDO> teamPerDOList = teamPerService.selectDetails(teamFarDO.getTeamId());
//        List<TeamPlanDO> teamPlanDOList = teamPlanService.selectDetails(teamFarDO.getTeamId());
        teamFarDO.setPerDOList(teamPerDOList);
//        teamFarDO.setPlanDOS(teamPlanDOList);
        return SystemResponse.success(teamFarDO);
    }

    /**
     * 废弃
     * @param openID
     * @return
     */
    @ApiOperation("查询个人创建的事项")
    @GetMapping("/selectByOneTeam")
    public BaseResponse selectByOneTeam(@RequestParam String openID){
        List<TeamFarDO> teamFarDOS = teamFarService.selectByOneTeam(openID);
        return SystemResponse.success(getTeamFarDOS(teamFarDOS),getTeamFarDOS(teamFarDOS).size());
    }

    @ApiOperation("查询个人参与与创建的事项")
    @GetMapping("/selectByOneJoinTeam")
    public BaseResponse selectByOneJoinTeam(@RequestParam String openID){
//        List<TeamFarDO> teamFarDOS = teamFarService.selectAll();
        List<TeamFarDO> teamFarDOS = teamFarService.selectByOneTeam(openID);
        List<TeamPerDO> teamPerDOS = teamPerService.selectByOneJoinTeam(openID);
        List<String> strings = teamFarDOS.stream().map(TeamFarDO::getTeamId).collect(Collectors.toList());
        List<String> stringList = teamPerDOS.stream().map(TeamPerDO::getTeamId).collect(Collectors.toList());
        List<String> newList = new ArrayList<>();
        for (String s : stringList) {
            newList.add(s);
        }
        for (String string : strings) {
            newList.add(string);
        }
        List<String> collect1 = newList.stream().distinct().collect(Collectors.toList());
        List<TeamFarDO> list = new ArrayList<>();
        for (String s : collect1) {
            TeamFarDO teamFarDO = teamFarService.selectDetailsByTeamID(s);
            List<TeamPerDO> teamPerDOS1 = teamPerService.selectDetails(s);
            teamFarDO.setPerDOList(teamPerDOS1);
            list.add(teamFarDO);
        }
        List<PlanStatusCountDTO> count = teamFarService.selectStatusNumbers(openID);

        HashMap<String, Object> map = new HashMap<>();
        map.put("all",list);
        map.put("count", count);
        return SystemResponse.success(map);
    }


    @ApiOperation("退出团队")
    @GetMapping("/returnTeam")
    public BaseResponse returnTeam(@RequestParam String id,@RequestParam String openID){
        int i = teamPerService.returnTeam(id,openID);
        if (i >= 1) {
            return SystemResponse.success();
        } else {
            return SystemResponse.fail("退出团队失败");
        }
    }

    @ApiOperation("删除团队")
    @GetMapping("/deleteTeam")
    public BaseResponse deleteTeam(@RequestParam String id){
        int i = teamFarService.deleteTeam(id);
        if (i == 1) {
            return SystemResponse.success();
        } else {
            return SystemResponse.fail("删除团队失败");
        }
    }

    @ApiOperation("更新团队")
    @PostMapping("/updateTeam")
    public BaseResponse updateTeam(@RequestBody TeamFarDO teamFarDO){
        int i = teamFarService.updateTeam(teamFarDO);
        if (i == 1) {
            return SystemResponse.success();
        } else {
            return SystemResponse.fail("删除团队失败");
        }
    }





    private List<TeamFarDO> getTeamFarDOS(List<TeamFarDO> teamFarDOS) {
        List<TeamPerDO> teamPerDOS = teamPerService.selectAll();
        List<TeamPlanDO> teamPlanDOS = teamPlanService.selectAll();
        eachCheck(teamFarDOS, teamPerDOS, teamPlanDOS);
        return  teamFarDOS;
    }

    private void eachCheck(List<TeamFarDO> teamFarDOS, List<TeamPerDO> teamPerDOS, List<TeamPlanDO> teamPlanDOS) {
        for (TeamFarDO teamFarDO : teamFarDOS) {
            List<TeamPerDO> teamPerDOList = teamPerDOS.stream().filter(s -> s.getTeamId().equals(teamFarDO.getTeamId())).collect(Collectors.toList());
            List<TeamPlanDO> teamPlanDOList = teamPlanDOS.stream().filter(s -> s.getTeamId().equals(teamFarDO.getTeamId())).collect(Collectors.toList());
            teamFarDO.setPerDOList(teamPerDOList);
            teamFarDO.setPlanDOS(teamPlanDOList);
        }
    }
}

